﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using ModelLayer;

namespace DBLayer
{
    public class DBShow
    {
        private SqlConnection con;

        //constructor
        public DBShow()
        {
            con = DBConnection.getInstance().getDBCon();
        }

        /**
         * Assemble a show
         * */
        public Show AssembleShow(ShowRoom room, ShowType type, List<Price> price, int id)
        {
            Show show = null;

            SqlDataReader reader = null;

            SqlParameter idParam = new SqlParameter("@id", SqlDbType.Int);
            idParam.Value = id;

            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandText = "SELECT Date, imageDir FROM Show WHERE id = @id";
            command.Parameters.Add(idParam);

            try
            {
                reader = command.ExecuteReader();

                while (reader.Read())
                {
                    //show = new Show(d, room, type, price, showId);
                    show = new Show(reader.GetDateTime(0), room, type, price, id, reader.GetString(1));
                    //show = new Show(d, room, type, price, showId, reader.GetString(1));
                }

                reader.Close();
            }
            catch (SqlException e)
            {
                Console.WriteLine(e);
            }
                return show;
        }

        /**
         * get a show by date and nothing else, assemble object in service
         * */
        public List<Show> GetShowsByDate(DateTime date)
        {
            Show show = null;
            ShowRoom room = null;
            ShowType type = null;
            List<Price> prices = null;
            string imageDir = "";

            List<Show> shows = new List<Show>();


            SqlDataReader reader = null;

            SqlParameter dateParam = new SqlParameter("@date", SqlDbType.DateTime);
            dateParam.Value = date;

            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandText = "SELECT show_Id FROM Show WHERE date = @date";
            command.Parameters.Add(dateParam);

            try
            {
                reader = command.ExecuteReader();

                while (reader.Read())
                {
                    shows.Add(show = new Show(date, room, type, prices, reader.GetInt32(0), imageDir));
                }

                reader.Close();
            }
            catch (SqlException e)
            {
                Console.WriteLine(e);
            }
            return shows;
        }

        public List<Show> GetAllShows()
        {
            DateTime date = new DateTime();
            Show show = null;
            ShowRoom room = null;
            ShowType type = null;
            List<Price> prices = null;
            string imageDir = "";

            List<Show> shows = new List<Show>();
            
            SqlDataReader reader = null;      

            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandText = "SELECT * FROM Show";
         

            try
            {
                reader = command.ExecuteReader();

                while (reader.Read())
                {
                    shows.Add(show = new Show(date, room, type, prices, reader.GetInt32(0), imageDir));
                }
                reader.Close();
            }
            catch (SqlException e)
            {
                Console.WriteLine(e);
            }
            return shows;
        }

    }
}
